<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getRoles } from '@/api/settings'
import { getUserInfoById, giveAssignRoles } from '@/api/user'
export default {
  props: {
    userId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    async hSubmit() {
      const res = await giveAssignRoles({ id: this.userId, roleIds: this.roleIds })
      console.log(res, '分配权限')
      this.$emit('success')
    },
    async loadRoles() {
      try {
        const res = await getRoles({ page: 1, pagesize: 100 })
        const userIds = await getUserInfoById(this.userId)
        console.log(userIds, '角色信息')
        this.roleIds = userIds.data.data.roleIds || [] // 数组为null的话会报错
        // console.log('getRoles', res)
        this.list = res.data.data.rows
        // 保存数据
      } catch (err) {
        console.log('getRoles', err)
      }
    },
    closeDialog() {

    }
  }
}
</script>
